import Paging from '../../../wxjs/Paging.js'
import navToNext from '../../../wxjs/navToNext.js'
const app = getApp();

var pag = new Paging();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  shareUuid: "", //分享对应动态的uuid
};

var currentPage = {
  data,
  reqUrl: app.serAddrs.contentList,
  onLoad,
  getUser,
  navToChat,
  clickFollow, //关注
  onShareAppMessage(res) {
    const that = this;
    const otherAccount = that.data.otherAccount;
    const uuid = res && res.target && res.target.dataset && res.target.dataset.uuid || "";
    let path = app.xhx_route.homeList + "?otherAccount=" + otherAccount + "&uuid=" + uuid;
    return {
      title: '',
      path,
    }
  }
};

var page = Object.assign(pag.func(),navToNext, currentPage);

Page(page)


function onLoad(options) {
  const that = this;
  const otherAccount = options && options.otherAccount || 0;
  const index = options && options.index || 0;
  that.setData({
    otherAccount,
    index,
    thatUserId: app.userInfo.userId || "",
  })
  that.listParams.otherAccount = otherAccount;
  that.getUser();
  // 初始化
  that.pageListInit();
}

function getUser() {
  const that = this;
  const otherAccount = that.data.otherAccount;

  var reqData = {
    otherAccount: otherAccount
  };

  app.middleware.requestAjax({
    serAddr: app.serAddrs.getPersonCenterInfo,
    data: reqData,
    success: function(res) {
      var temp = res.data;
      that.setData(temp)
      wx.setNavigationBarTitle({
        title: temp.username,
      })
    },
    fail: function(res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
}

function navToChat() {
  const that = this;
  const userId = that.data.userId;
  const username = that.data.username;
  const headPortrait = that.data.headPortrait;

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  if (userId == app.userInfo.userId) return;

  wx.navigateTo({
    url: app.xhx_route.tomeChat + "?username=" + username + "&headPortrait=" + headPortrait + "&receiver=" + userId,
  })
}

function clickFollow(event) {
  const that = this;
  var focusStatus = that.data.focusStatus;
  var userId = that.data.userId || 0;

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  var reqData = {
    focusAccount: userId,
  };
  var serAddr = app.serAddrs.addFocus;

  if (focusStatus == 1) {
    serAddr = app.serAddrs.delFocus;
  }

  app.middleware.requestAjax({
    serAddr: serAddr,
    data: reqData,
    success: function(res) {
      that.setData({
        focusStatus: focusStatus == 1 ? 0 : 1,
      })

      var prevPage = getCurrentPages()[getCurrentPages().length - 2];

      if (prevPage.route == "pages/mine/users/users") {
        var index = that.data.index;
        var lists = prevPage.data.lists || [];
        lists.splice(index, 1);
        prevPage.setData({
          lists,
          msg: lists.length == 0 ? "暂无关注" : "",
        })
      } else if (prevPage.route == "pages/home/details/details") {
        prevPage.setData({
          focusStatus: focusStatus == 1 ? 0 : 1,
        })
      }

    },
    complete: function(res) {
      app.middleware.showToast(res && res.msg || "操作失败");
    }
  })
}